<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn\Book;

use app\common\model\learn\book\CategoryUser as CategoryUserModel;
use app\api\model\learn\book\TitleUser as TitleUserModel;
use think\facade\Db;

/**
 * 商品评价模型
 * Class Article
 * @package app\api\model
 */
class CategoryUser extends CategoryUserModel {

	/**
	 * 获取文章列表
	 * @param int $categoryId
	 * @param int $limit
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getListComplate(int $userId) {
		// 检索查询条件
		$filter = [];
		$userId > 0 && $filter[] = ['user_id', '=', $userId];
		// 获取列表数据
		return $this
			->with(['book'])
			->where($filter)
			->where('is_delete', '=', 0)
			->where('status', '=', 10)
			->order(['create_time' => 'desc'])
			->select()->toArray();
	}

	//待学习

	public function getListWait(int $userId) {
		// 检索查询条件
		$filter = [];
		$userId > 0 && $filter[] = ['user_id', '=', $userId];
		// 获取列表数据
		$lists = $this
			->with(['book.image'])
			->where($filter)
			->where('is_delete', '=', 0)
			->where('status', '=', 0)
			->order(['create_time' => 'desc'])
			->select()->toArray();
		foreach ($lists as &$item) {
			$not_num = $item['total_num'] - $item['complate_num'];
			$day = round($not_num / $item['num'], 3);
			$complate_time = time() + $day * 86400;
			$item['complate_time'] = Date('Y-m-d', (int) $complate_time);
			$item['day'] = $day;
		}
		return $lists;
	}
	public function bookInfo($userId, int $bookId) {
		// 获取文章详情
		$detail = parent::detail(['user_id' => $userId, 'book_id' => $bookId], ['book.image']);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前书籍不存在');
		}
		$detail['review_num'] = Db::name('learn_article_title_user')
			->where('user_id', $userId)->where('book_id', $bookId)->where('review_num', '>', 0)->count();
		//今天查看新条文
		$detail['complate_num_day'] = Db::name('learn_article_title_user')
			->where('user_id', $userId)
			->where('date', Date('Y-m-d'))
			->where('book_id', $bookId)->where('status', '>', 0)->count();

		//今天复习条文
		$detail['review_num_day'] = Db::name('learn_article_title_review')
			->where('user_id', $userId)->where('date', date('Y-m-d'))->where('book_id', $bookId)->count();

		//今日学习时间
		$time = Db::name('learn_article_title_user')
			->where('user_id', $userId)->where('book_id', $bookId)->where('date', Date('Y-m-d'))->sum('time');
		$hour = round($time / 3600);

		$second = $time % 3600;
		$minute = round($second / 60);

		$time = $hour > 0 ? $hour . 'H' : '';
		$time .= $minute . 'min';
		$detail['time'] = $time;

		//打卡天数
		$detail['day'] = Db::name('learn_article_title_user')
			->where('user_id', $userId)->where('book_id', $bookId)->group('date')->count();

		return $detail;
	}

	public function getTimu($userId, int $bookId) {
		// 获取文章详情
		$detail = parent::detail(['user_id' => $userId, 'book_id' => $bookId], ['book']);
		if (empty($detail) || $detail['is_delete']) {
			throwError('很抱歉，当前书籍不存在');
		}
		//查询今日答题记录
		$log = Db::name('learn_article_title_user')->alias('u')
			->join('learn_article_title_user t', 't.article_id=u.article_id')
			->where('u.user_id', $userId)->where('u.book_id', $bookId)
			->where('u.date', Date('Y-m-d'))
			->field('u.status,t.*')
			->select()->toArray();
		Db::name('learn_article_title_user')
			->where('date', '<', Date('Y-m-d'))
			->where('user_id', $userId)
			->where('review_num', 0)
			->where('status', 0)
			->where('book_id', $bookId)
			->delete();
		if ($log) {

		} else {
			$logAll = Db::name('learn_article_title_user')
				->where('user_id', $userId)
				->where('book_id', $bookId)
				->field('article_id')
				->select()->toArray();
			$ids = array_column($logAll, 'article_id');

			$log = Db::name('learn_article_title')->where('article_id', 'not in', $ids)->limit($detail['num'])->select()->toArray();
			$insertData = [];
			foreach ($log as $key => $value) {
				$insertData[] = ['store_id' => $value['store_id'], 'date' => Date('Y-m-d'), 'user_id' => $userId, 'book_id' => $bookId, 'article_id' => $value['article_id'], 'create_time' => time()];
			}

			Db::name('learn_article_title_user')->insertAll($insertData);

		}

		$id = $log[0]['article_id'];

		return $id;
	}

	public function getCategoryTimu($userId, $param) {
		$bookName = Db::name('learn_book_category')->where('category_id', $param['bookId'])->value('name');
		$bookName2 = Db::name('learn_book_category')->where('category_id', $param['categoryId'])->value('name');

		//查询是否新增题目
		$book_user = Db::name('learn_book_user')
			->where('category_id', $param['categoryId'])
			->where('user_id', $userId)->find();
		$insertData = [];
		if (!$book_user) {
			$list = Db::name('learn_book')
				->where('book_name', $bookName)
				->where('book_name2', $bookName2)
				->select()->toArray();
			foreach ($list as $key => $value) {
				$data['create_time'] = time();
				$data['store_id'] = self::$storeId;
				$data['book_name2'] = $bookName2;
				$data['book_id'] = $param['bookId'];
				$data['category_id'] = $param['categoryId'];
				$data['article_id'] = $value['subject_id'];
				$data['user_id'] = $userId;
				$data['sort'] = $key + 1;

				$insertData[] = $data;
			}

			Db::name('learn_book_user')->insertAll($insertData);
		}
		$filter = [];

		if ($param['checkType'] != '全部') {

			$filter[] = ['b.type', '=', $param['checkType']];
		}
		if ($param['checkYear'] > 0) {

			if (isset($param['startYear'])) {
				$filter[] = ['b.year', '>=', $param['startYear']];
				$filter[] = ['b.year', '<=', $param['endYear']];
			} else {
				$year = Date('Y');
				$start = $year - $param['checkYear'];
				$filter[] = ['b.year', '>=', $start];
			}

		}
		$filter[] = ['u.category_id', '=',$param['categoryId']];
		$model = new TitleUserModel;

		// $list = Db::name('learn_book_user')->alias('u')
		// 	->join('learn_book b', 'b.subject_id=u.article_id')
		// 	->where($filter)
		// 	->where('u.category_id', $param['categoryId'])
		// 	->where('user_id', $userId)
		// 	->field('u.article_id,u.sort')
		// 	->select()->toArray();

		$list = $model->getList($filter);

		return $list;
	}
	public function setting($userId, int $bookId) {

		$list = $this->where('parent_id', $bookId)
		->where('user_id', $userId)->select()->toArray();

		// foreach ($list as &$item) {
		// 	$item['list'] = Db::name('learn_book_user')->where('user_id', $userId)
		// 		->where('book_name2', $item['name'])->select()->toArray();
		// }

		return $list;
	}
}